package com.ruoyi.commodity.controller;


import com.ruoyi.commodity.domain.CommodityType;
import com.ruoyi.commodity.domain.vo.CommodityTypeQueryVo;
import com.ruoyi.commodity.service.ICommodityTypeService;
import com.ruoyi.common.core.constant.CommodityConstants;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.security.utils.SecurityUtils;
import org.apache.commons.lang3.ArrayUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;


@RestController
@RequestMapping("/commoditytype")
public class CommodityTypeController extends BaseController {

    @Autowired
    private ICommodityTypeService commodityTypeService;

//
    @PostMapping
    public AjaxResult add(@RequestBody CommodityType commodityType) {
        if (!commodityTypeService.checkCommodityTypeNameUnique(commodityType))
        {
            return error("新增商品类型'" + commodityType.getCommodityTypeName() + "'失败，商品类型名称已存在");
        }
        commodityType.setCreateBy(SecurityUtils.getUsername());
        return toAjax(commodityTypeService.insertCommodityType(commodityType));
    }


    @GetMapping(value = "/{commodityTypeId}")
    public AjaxResult getInfo(@PathVariable Long commodityTypeId)
    {
        return success(commodityTypeService.selectCommodityTypeById(commodityTypeId));
    }

    @GetMapping("/list")
    public AjaxResult list(CommodityTypeQueryVo commodityType)
    {
        List<CommodityType> list = commodityTypeService.selectCommodityTypeList(commodityType);
        return success(list);
    }

    /**
     * 查询列表（排除节点）
     */
    @GetMapping("/list/exclude/{commodityTypeId}")
    public AjaxResult excludeChild(@PathVariable(value = "commodityTypeId", required = false) Long commodityTypeId)
    {
        List<CommodityType> list = commodityTypeService.selectCommodityTypeList(new CommodityTypeQueryVo());
        list.removeIf(c -> c.getCommodityTypeId().intValue() == commodityTypeId || ArrayUtils.contains(StringUtils.split(c.getAncestors(), ","), commodityTypeId + ""));
        return success(list);
    }
    /**
     * 修改部门
     */
    @PutMapping
    public AjaxResult Update(@Validated @RequestBody CommodityType commodityType)
    {
        Long  commodityTypeId= commodityType.getCommodityTypeId();
        if (!commodityTypeService.checkCommodityTypeNameUnique(commodityType))
        {
            return error("修改商品类型'" + commodityType.getCommodityTypeName() + "'失败，商品类型名称已存在");
        }
        else if (commodityType.getParentId()!= null&&commodityType.getParentId().equals(commodityTypeId))
        {
            return error("修改商品类型'" + commodityType.getCommodityTypeName() + "'失败，上级商品类型不能是自己");
        }
        else if (StringUtils.equals(String.valueOf(CommodityConstants.TYPE_DISABLE),String.valueOf(commodityType.getStatus())) && commodityTypeService.selectNormalChildrenCommodityTypeById(commodityTypeId) > 0)
        {
            return error("该商品类型包含未停用的子商品类型！");
        }
        commodityType.setUpdateBy(SecurityUtils.getUsername());
        System.out.println(commodityType.getStatus());
        return toAjax(commodityTypeService.updateCommodityType(commodityType));
    }
    /**
     * 删除
     */

    @DeleteMapping("/{commodityTypeId}")
    public AjaxResult remove(@PathVariable Long commodityTypeId)
    {
        if (commodityTypeService.hasChildByCommodityTypeId(commodityTypeId))
        {
            return warn("存在下级商品类型,不允许删除");
        }
        return toAjax(commodityTypeService.deleteCommodityTypeById(commodityTypeId));
    }
}
